package com.LCQ.crm.workbench.controller;

import com.LCQ.crm.settings.domain.User;
import com.LCQ.crm.utils.*;
import com.LCQ.crm.vo.PaginationVo;
import com.LCQ.crm.workbench.domain.Activity;
import com.LCQ.crm.workbench.domain.ActivityRemark;
import com.LCQ.crm.workbench.service.ActivityService;
import com.LCQ.crm.workbench.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("!!!!ActivityService!!!!");
        String reqUri = request.getRequestURI();

        if(reqUri==null){
            return;
        }

        if(reqUri.contains("save.do")){
            save(request,response);
            return;
        }else if(reqUri.contains("getPage.do")){
            getPage(request,response);
            return;
        }else if(reqUri.contains("getGlobal.do")){
            getGlobal(request,response);
            return;
        }else if(reqUri.contains("getPageList.do")){
            getPageList(request,response);
            return;
        }else if(reqUri.contains("delete.do")){
            delete(request,response);
            return;
        }else if(reqUri.contains("getActivityById.do")){
            getActivityById(request,response);
            return;
        }else if(reqUri.contains("update.do")){
            //更新活动
            update(request,response);
            return;
        }else if(reqUri.contains("detail.do")){
            //详细信息
            detail(request,response);
            return;
        }else if(reqUri.contains("getRemarkByAId.do")){
            //获取备注信息
            getRemarkByAId(request,response);
            return;
        }else if(reqUri.contains("deleteRemark.do")){
            //删除备注
            deleteRemark(request,response);
            return;
        }else if(reqUri.contains("updateRemark.do")){
            //删除备注
            updateRemark(request,response);
            return;
        }else if(reqUri.contains("addRemark.do")){
            //删除备注
            addRemark(request,response);
            return;
        }
    }

    private void addRemark(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(UUIDUtil.getUUID());
        activityRemark.setNoteContent(request.getParameter("content"));
        activityRemark.setActivityId(request.getParameter("activityId"));
        activityRemark.setCreateBy(((User)(request.getSession().getAttribute("user"))).getName());
        String sysTime = DateTimeUtil.getSysTime();
        activityRemark.setCreateTime(sysTime);
        activityRemark.setEditTime(sysTime);//方便排序，在创建时也填上更新时间
        activityRemark.setEditFlag("0");
        boolean result = activityService.addRemark(activityRemark);
        if(result){
            Map map = new HashMap();
            map.put("success",true);
            map.put("remark",activityRemark);
            PrintJson.printJsonObj(response,map);
        }else {
            PrintJson.printJsonFlag(response, false);
        }
    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(request.getParameter("id"));
        activityRemark.setNoteContent(request.getParameter("content"));
        activityRemark.setEditBy(((User)(request.getSession().getAttribute("user"))).getName());
        activityRemark.setEditTime(DateTimeUtil.getSysTime());
        activityRemark.setEditFlag("1");
        boolean result = activityService.updateRemark(activityRemark);

        if(result){
            Map map = new HashMap();
            map.put("success",true);
            map.put("remark",activityRemark);
            PrintJson.printJsonObj(response,map);
        }else {
            PrintJson.printJsonFlag(response, false);
        }
    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean result = activityService.deleteRemark(id);
        if(result){
            PrintJson.printJsonFlag(response, true);
        }else {
            PrintJson.printJsonFlag(response, false);
        }

    }

    private void getRemarkByAId(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        List<ActivityRemark> list = activityService.getRemarkByAId(id);
        PrintJson.printJsonObj(response,list);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity =  activityService.getDetail(id);
        request.setAttribute("activity", activity);
        try {
            request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = new Activity();
        activity.setId(request.getParameter("id"));
        activity.setOwner(request.getParameter("owner"));
        activity.setName(request.getParameter("name"));
        activity.setStartDate(request.getParameter("startDate"));
        activity.setEndDate(request.getParameter("endDate"));
        activity.setDescription(request.getParameter("description"));
        activity.setCost(request.getParameter("cost"));
        activity.setEditTime(DateTimeUtil.getSysTime());
        activity.setEditBy(((User)(request.getSession().getAttribute("user"))).getName());
        int result = activityService.update(activity);
        if(result>0){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void getActivityById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = activityService.getActivityById(id);
        PrintJson.printJsonObj(response,activity);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("id");
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean result = activityService.delete(ids);
        if(result){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }
    }

    //获取一页的信息有条件查询
    private void getPageList(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        int index = (pageNum-1)*pageSize;
        Map map = new HashMap();
        map.put("index",index);
        map.put("pageSize",pageSize);
        map.put("owner",request.getParameter("owner"));
        map.put("name",request.getParameter("name"));
        map.put("startDate",request.getParameter("startDate"));
        map.put("endDate",request.getParameter("endDate"));
        PaginationVo<Activity> pageList =  activityService.getPageList(map);
        PrintJson.printJsonObj(response,pageList);
    }

    //获取全部记录数量
    private void getGlobal(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService)ServiceFactory.getService(new ActivityServiceImpl());
        Map map  = activityService.getGlobal();
        PrintJson.printJsonObj(response,map);
    }

    //获取一页的信息无条件
    private void getPage(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        List<Map> listMap = activityService.getPage(pageNum,pageSize);
        PrintJson.printJsonObj(response,listMap);
    }

    //添加一条信息
    private void save(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = new Activity();
        activity.setId(UUIDUtil.getUUID());
        activity.setOwner(request.getParameter("owner"));
        activity.setName(request.getParameter("name"));
        activity.setStartDate(request.getParameter("startDate"));
        activity.setEndDate(request.getParameter("endDate"));
        activity.setDescription(request.getParameter("description"));
        activity.setCost(request.getParameter("cost"));
        activity.setCreateTime(DateTimeUtil.getSysTime());
        activity.setCreateBy(((User) request.getSession().getAttribute("user")).getName());
        int result = activityService.add(activity);
        if(result>0){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }
    }
}
